package com.example.saifly.inPageRecord.BodyHistoryData;

import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.common.Activity;
import com.example.common.Application.MyApplication;
import com.example.common.Retrofit.RetrofitHelper;
import com.example.saifly.R;
import com.example.saifly.inPageRecord.RecordModel.BodyData;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.http.Body;
import retrofit2.http.Headers;
import retrofit2.http.POST;

public class BodyHistoryBloodOxygenActivity extends Activity {
    @Override
    protected int getContentLayoutId()
    {
        return R.layout.activity_bloodoxygen_history;
    }
    private ImageView ivReturn;
    private int bloodOxygenData[]=new int[5];
    private String inputTime[]=new String[5];
    private List<BodyData> BloodOxygenList;

    private TextView tvInputTime1;
    private TextView tvInputTime2;
    private TextView tvInputTime3;
    private TextView tvInputTime4;
    private TextView tvInputTime5;
    private TextView tvBloodOxygenData1;
    private TextView tvBloodOxygenData2;
    private TextView tvBloodOxygenData3;
    private TextView tvBloodOxygenData4;
    private TextView tvBloodOxygenData5;

    private SharedPreferences sharedPreferences= MyApplication.getContext().getSharedPreferences("config", Context.MODE_PRIVATE);

    @Override
    protected void init()
    {
        super.init();

        BloodOxygenList =new ArrayList<>();

        ivReturn = findViewById(R.id.bloodOxygenData_return);
        tvInputTime1=findViewById(R.id.bloodOxygenTime1);
        tvInputTime2=findViewById(R.id.bloodOxygenTime2);
        tvInputTime3=findViewById(R.id.bloodOxygenTime3);
        tvInputTime4=findViewById(R.id.bloodOxygenTime4);
        tvInputTime5=findViewById(R.id.bloodOxygenTime5);
        tvBloodOxygenData1=findViewById(R.id.bloodOxygenData1);
        tvBloodOxygenData2=findViewById(R.id.bloodOxygenData2);
        tvBloodOxygenData3=findViewById(R.id.bloodOxygenData3);
        tvBloodOxygenData4=findViewById(R.id.bloodOxygenData4);
        tvBloodOxygenData5=findViewById(R.id.bloodOxygenData5);

        for(int i=0;i<5;i++)
        {
            bloodOxygenData[i]=0;
            inputTime[i]="未录入";
        }

        new Thread(new Runnable() {
            @Override
            public void run()
            {
                bloodOxygenHistory(sharedPreferences.getString("uid","default"));
            }
        }).start();

        doJump();

    }

    //getTopBodyData   接口名是唯一的，和服务器中的相同
    private interface getTopBodyData {
        @Headers({"Content-Type:application/json"})
        @POST("/estimate/getTopBodyData")    //路由也是唯一的，是读取数据的路径
        Call<ResponseBody> bloodOxygenHistory(@Body JSONObject jsonObject);
    }

    private void bloodOxygenHistory(String phoneNumber) {
       RetrofitHelper retrofitHelper=new RetrofitHelper();
       Retrofit retrofit=retrofitHelper.getRetrofit();
       getTopBodyData apiBloodOxygenHistory=retrofit.create(getTopBodyData.class);

       JSONObject jsonObject=new JSONObject();
       jsonObject.put("patientPhone",phoneNumber);  //向服务器传递的数据，这是与服务器约定好的
        Call<ResponseBody> call =apiBloodOxygenHistory.bloodOxygenHistory(jsonObject);
        final Response<ResponseBody> response;
        try {
            response=call.execute();
            String jsonResult =response.body().string();
            JSONObject result=JSON.parseObject(jsonResult);
            Log.d("top55", "bloodOxygenHistory: "+result);

            if(result.getInteger("code")==1)
            {
                List<BodyData> bodyData=result.getJSONArray("result").toJavaList(BodyData.class);
                for(int i=0;i<bodyData.size();i++)
                {
                    Date tempTime=bodyData.get(i).getDate();
                    SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd");
                    inputTime[i]=dateFormat.format(tempTime);
                    bloodOxygenData[i]=bodyData.get(i).getBloodOxygen();
                }
                if(bloodOxygenData[0]==0)  inputTime[0]="未录入";
                showBloodOxygen();
            }
            else
            {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast toast=Toast.makeText(BodyHistoryBloodOxygenActivity.this,"血氧饱和度数据获取失败，请重试",Toast.LENGTH_LONG);
                        showMyToast(toast,10*1000);   //显示10秒
                    }
                });
            }
        } catch (IOException e) {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(BodyHistoryBloodOxygenActivity.this, "服务器在开小差儿", Toast.LENGTH_SHORT).show();
                }
            });
            e.printStackTrace();
        }
    }

    public void showBloodOxygen()
    {
        tvInputTime1.setText(inputTime[0]);
        tvInputTime2.setText(inputTime[1]);
        tvInputTime3.setText(inputTime[2]);
        tvInputTime4.setText(inputTime[3]);
        tvInputTime5.setText(inputTime[4]);
        tvBloodOxygenData1.setText(String.valueOf(bloodOxygenData[0])+"%");
        tvBloodOxygenData2.setText(String.valueOf(bloodOxygenData[1])+"%");
        tvBloodOxygenData3.setText(String.valueOf(bloodOxygenData[2])+"%");
        tvBloodOxygenData4.setText(String.valueOf(bloodOxygenData[3])+"%");
        tvBloodOxygenData5.setText(String.valueOf(bloodOxygenData[4])+"%");
    }

    //Toast 自定义显示时长
    public void showMyToast(final Toast toast, final int cnt) {
        final Timer timer =new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                toast.show();
            }
        },0,3000);
        new Timer().schedule(new TimerTask() {
            @Override
            public void run() {
                toast.cancel();
                timer.cancel();
            }
        }, cnt );
    }

    private void doJump()
    {
        ivReturn.setOnClickListener(view -> finish());
    }
}
